package com.zlx.xbhy.config;

import com.zlx.xbhy.entity.Meeting;
import com.zlx.xbhy.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SaticScheduleTask
 * @Description 定时任务，定时刷新会议状态
 * @Author Edward
 * @Date 2020/11/23 0:36
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {
    @Autowired
    private MeetingService meetingService;
    private boolean flag = false;

//    //3.添加定时任务
//    @Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：5秒
    @Scheduled(fixedRate=1000*60)
    private void configureTasks() {
        if (!flag) {
            flag = true;
//            List<Meeting> allInfoRToUpdateStatus = meetingService.selectAll();
            List<Meeting> allInfoRToUpdateStatus = meetingService.selectAlls();
            for (Meeting info : allInfoRToUpdateStatus) {
                if (info.getStatus().equals(2L)) {
                    //如果会议已经结束就不会再更新
                    continue;
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date endTime = info.getEndTime();
                Date startTime = info.getStartTime();
                if (endTime.getTime() < new Date().getTime()) {
                    //会议结束
                    info.setStatus(2L);
                    meetingService.updateByPrimaryKeySelective(info);
                } else if (startTime.getTime() > new Date().getTime()) {
                    //会议未开始
                    info.setStatus(0L);
                    meetingService.updateByPrimaryKeySelective(info);
                } else if (startTime.getTime() < new Date().getTime() && endTime.getTime() > new Date().getTime()) {
                    //会议进行中
                    info.setStatus(1L);
                    meetingService.updateByPrimaryKeySelective(info);
                }

            }
            flag = false;
        }
    }
}